﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LeMigue.TeamPortal.Base.Biz.Interface;
using LeMigue.TeamPortal.Base.View;
using LeMigue.Scrum.Iterface.Model;
using LeMigue.Scrum.Iterface.Service;
using LeMigue.TeamPortal.Base.Service;
using System.Web;
using LeMigue.Framework.Utils;


namespace LeMigue.TeamPortal.Base.Biz.Implement
{
    public class TeamBiz : ITeamBiz
    {
        ITeamService _teamService = FactoryService.TeamService;

        public IList<TeamView> List(Team ativo)
        {
            return _teamService.List().Select(t => new TeamView(t, ativo != null && t.Id == ativo.Id)).ToList();
        }


        public TeamView TeamView(Team team, bool ativo)
        {
            return new TeamView(team, ativo);
        }


        public Team Load(int? id)
        {
            if (!id.HasValue)
            {
                return null;
            }
            //TODO: ver se não teria um local melhor para colocar, quem sabe cachear ou até mesmo um local mais "global"
            User usuario = FactoryBiz.CurrentUser.GetUser(HttpContext.Current);

            Team team = Cache.Get<Team>(usuario.Ref, "Team");

            if (team != null && team.Id == id)
                return team;

            Cache.Remove(usuario.Ref, "Team");

            team = _teamService.Load(id.Value);

            Cache.Set<Team>(usuario.Ref, "Team", team);

            return team;
        }
    }
}
